#!/usr/bin/env python
# Filename: commons.py
import sys,urllib,re,md5,os
import MySQLdb
import ConfigParser
class mycommon:
    #get file code
    def getHtml(self,domain):
        try:
            wp = urllib.urlopen(domain)
        except:
            print 'url exception'
        content = wp.read()
        wp.close()
        return content
    
    #get config file
    def initConfig(self,configFile):
        config = ConfigParser.ConfigParser();
        config.readfp(open(configFile));
        return config;
    
    def matchCode(self,regular,content):
        str = re.compile(regular,re.I|re.S)
        return str.findall(content)
    
    def dbConn(self,dbServer="default"):
        config = self.initConfig("../config/_db.ini")
        user = str(config.get(dbServer,"user"))
        db   = str(config.get(dbServer,"db"))
        passwd   = str(config.get(dbServer,"passwd"))
        host   = str(config.get(dbServer,"host"))
        conn = MySQLdb.connect(user=user, db=db, passwd=passwd, host=host)
        return conn